package cn.dw.utils;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class ConditionUtil {
	private Order order;
	private PagerModel pageModel;
	List<ConditionType> conditionTypes = new ArrayList<>();

	public Order getOrder() {
		return this.order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public PagerModel getPageModel() {
		return this.pageModel;
	}

	public void setPageModel(PagerModel pageModel) {
		this.pageModel = pageModel;
	}

	public List<ConditionType> getConditionTypes() {
		return this.conditionTypes;
	}

	public void setConditionTypes(List<ConditionType> conditionTypes) {
		this.conditionTypes = conditionTypes;
	}

	private Criterion getCondition(ConditionType ct) {
		if (ct != null) {
			int oper = ct.getIoperator();
			if (oper == 0) {
				return Restrictions.eq(ct.getName(), ct.getValue());
			}
			if (8 == oper) {
				return Restrictions.ne(ct.getName(), ct.getValue());
			}
			if (2 == oper) {
				return Restrictions.ge(ct.getName(), ct.getValue());
			}
			if (6 == oper) {
				return Restrictions.gt(ct.getName(), ct.getValue());
			}
			if (1 == oper) {
				return Restrictions.le(ct.getName(), ct.getValue());
			}
			if (7 == oper) {
				return Restrictions.lt(ct.getName(), ct.getValue());
			}
			if (4 == oper) {
				return Restrictions.isNull(ct.getName());
			}
			if (5 == oper) {
				return Restrictions.isNotNull(ct.getName());
			}
			if (3 == oper) {
				return Restrictions.like(ct.getName(), ct.getValue().toString(), MatchMode.START);
			}
			if (9 == oper) {
				return Restrictions.in(ct.getName(), ct.getValues());
			}
			if (10 == oper) {
				return Restrictions.eqProperty(ct.getName(), ct.getOtherName());
			}
			if (11 == oper) {
				return Restrictions.leProperty(ct.getName(), ct.getOtherName());
			}
			if (12 == oper) {
				return Restrictions.geProperty(ct.getName(), ct.getOtherName());
			}
			if (13 == oper) {
				return Restrictions.gtProperty(ct.getName(), ct.getOtherName());
			}
			if (14 == oper) {
				return Restrictions.ltProperty(ct.getName(), ct.getOtherName());
			}
			if (15 == oper) {
				return Restrictions.neProperty(ct.getName(), ct.getOtherName());
			}
		}
		return null;
	}

	public List<Criterion> criterion() {
		List<Criterion> criterions = new ArrayList<>();
		for (int i = 0; i < this.conditionTypes.size(); i++) {
			ConditionType ct = (ConditionType) this.conditionTypes.get(i);
			if (ct.getLinkType() != 12) {
				criterions.add(getCondition(ct));
			} else {
				Criterion tCriterion = null;
				Disjunction dj = Restrictions.disjunction();
				List<ConditionType> ors = ct.getConditionTypes();
				for (int j = 0; j < ors.size(); j++) {
					tCriterion = dj.add(getCondition((ConditionType) ors.get(j)));
				}
				if (tCriterion != null) {
					criterions.add(tCriterion);
				}
			}
		}
		return criterions;
	}

	public ConditionUtil add(ConditionType ct) {
		this.conditionTypes.add(ct);
		return this;
	}

	public ConditionUtil addOrderUtil(Order order) {
		this.order = order;
		return this;
	}

	public ConditionUtil addFetch(int pageNo, int pageSize) {
		this.pageModel = new PagerModel(pageNo, pageSize);
		return this;
	}
}
